package com.insyte.networks;

import android.util.Log;

import com.insyte.model.CategoryOfInterest;
import com.insyte.model.InteresetObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 22/06/2015.
 */
public class ParserUtils {
    private static final String TAG = "ParserUtils";

    public static List<CategoryOfInterest> parerInterest(String str) {
        List<CategoryOfInterest> categoryOfInterests = new ArrayList<CategoryOfInterest>();
        try {
            JSONObject jsonObject = new JSONObject(str);
            JSONObject objValue = getJsonObject(jsonObject, "data");
            String dataValue = getStringValue(jsonObject, "data").replace("\",", ":");
            dataValue = dataValue.replace("\"", "");
            dataValue = dataValue.replace("{", "");
            String[] arrData = dataValue.split(":");
            for (int i = 0; i < arrData.length; i++) {
                if (i % 2 == 0) {
                    categoryOfInterests.add(new CategoryOfInterest(arrData[i]));
                    Log.d(TAG, arrData[i]);
                }
            }
            for (CategoryOfInterest item : categoryOfInterests) {
                String values = getStringValue(objValue, item.getName().replace("{", "").replace("\"", ""));
                item.setChildItemList(getChildItemList(values));
                Log.d(TAG, values);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return categoryOfInterests;
    }

    public static List<CategoryOfInterest> parerInterest(JSONObject jsonObject) {
        ArrayList<CategoryOfInterest> categoryOfInterests = new ArrayList<CategoryOfInterest>();
        try {
            JSONObject objValue = getJsonObject(jsonObject, "data");
            Iterator<String> iter = objValue.keys();
            while (iter.hasNext()) {
                String key = iter.next();
                try {
                    String values = getStringValue(objValue, key);
                    List<InteresetObject> arrayList = getChildItemList(values);
                    categoryOfInterests.add(new CategoryOfInterest(key, arrayList));
                } catch (Exception e) {
                    // Something went wrong!
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return categoryOfInterests;
    }

    public static List<InteresetObject> getChildItemList(String arrItem) {
        List<InteresetObject> arrayList = new ArrayList<InteresetObject>();
        String[] arrData = arrItem.split(",");
        for (String string : arrData) {
            arrayList.add(new InteresetObject(string));
        }
        return arrayList;
    }

    public static JSONObject getJsonObject(JSONObject obj, String name) {
        JSONObject jsonObject = new JSONObject();
        try {
            String str = obj.getString(name);
            jsonObject = new JSONObject(str);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            Log.d(TAG, "JSONObject width" + name + " is null");
        }
        return jsonObject;
    }

    public static JSONArray getJsonArray(JSONObject obj, String name) {
        JSONArray jsonArray = new JSONArray();
        try {
            jsonArray = obj.getJSONArray(name);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            Log.d(TAG, "array width" + name + " is null");
        }
        return jsonArray;
    }

    public static String getStringValue(JSONObject obj, String key) {
        try {
            return obj.isNull(key) ? "" : obj.getString(key);
        } catch (JSONException e) {
            return "";
        }
    }

    /**
     * Get long value
     *
     * @param obj
     * @param key
     * @return
     */
    public static long getLongValue(JSONObject obj, String key) {
        try {
            return obj.isNull(key) ? 0L : obj.getLong(key);
        } catch (JSONException e) {
            return 0L;
        }
    }

    public static int getIntValue(JSONObject obj, String key) {
        try {
            return obj.isNull(key) ? 0 : obj.getInt(key);
        } catch (JSONException e) {
            return 0;
        }
    }

    public static Double getDoubleValue(JSONObject obj, String key) {
        double d = 0.0;
        try {
            return obj.isNull(key) ? d : obj.getDouble(key);
        } catch (JSONException e) {
            return d;
        }
    }

    public static boolean getBooleanValue(JSONObject obj, String key) {
        try {
            return obj.isNull(key) ? false : obj.getBoolean(key);
        } catch (JSONException e) {
            return false;
        }
    }

    @SuppressWarnings("deprecation")
    public static String JsonDateToDate(String jsonDate) {
        // "/Date(1321867151710)/"
        int idx1 = jsonDate.indexOf("(");
        int idx2 = jsonDate.indexOf(")");
        String s = jsonDate.substring(idx1 + 1, idx2);
        long l = Long.valueOf(s);
        Date result = new Date(l);
        int Year = result.getYear() + 1900;
        return result.getDate() + "/" + result.getMonth() + "/" + Year;
    }
    //----------*******************CORE FUNCTION**************--------//
}
